<?php
/**
 * @var \yii\web\View $view
 */

use yii\helpers\ArrayHelper;
use common\models\db\Menu;
use common\models\db\RoleMenu;
use common\models\db\Role;
use common\models\db\RoleUser;
use common\models\db\User;
use pantera\textAvatar\TextAvatarWidget;
use yii\helpers\Html;

$items = [
    ['label' => '菜单', 'options' => ['class' => 'header']],
];
if (YII_ENV != 'prod' && \Yii::$app->user->identity->is_super) {
    $items[] = [
        'label' => 'DevTools',
        'icon' => 'bug',
        'url' => '#',
        'items' => [
            ['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']],
            ['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']],
            ['label' => 'logreader', 'icon' => 'dashboard', 'url' => ['/logreader']],
        ],
    ];
}
/*$api_menus = [
    [
        'label' => 'RBAC',
        'icon' => 'american-sign-language-interpreting',
        'url' => '#',
        'items' => [
            ['label' => '用户管理', 'icon' => 'users', 'url' => ['/rbac/user']],
            ['label' => '角色管理', 'icon' => 'universal-access', 'url' => ['/rbac/role']],
            ['label' => 'yii2应用管理', 'icon' => 'cubes', 'url' => ['/rbac/yii2-app']],
            ['label' => '菜单管理', 'icon' => 'navicon', 'url' => ['/rbac/menu']],
            ['label' => '动作管理', 'icon' => 'hand-pointer-o', 'url' => ['/rbac/action']],
        ],
    ],
];*/
if (!\Yii::$app->user->identity->is_super) {
    $menus_query = Menu::find()->alias('menu')->select('menu.*')
        ->leftJoin(RoleMenu::tableName() . " as rm", 'menu.id = rm.menu_id')
        ->leftJoin(Role::tableName() . " as role", 'rm.role_id = role.id')
        ->leftJoin(RoleUser::tableName() . " as ru", 'role.id = ru.role_id')
        ->leftJoin(User::tableName() . " as user", 'ru.user_id = user.id')
        ->where(['user.id' => \Yii::$app->user->id, 'menu.yii2_app_id' => \Yii::$app->appinfo->yii2App->id]);
} else {
    $menus_query = Menu::find()->alias('menu')->where(['yii2_app_id' => Yii::$app->appinfo->yii2App->id]);
}
$menus = $menus_query->andWhere(['active' => 1])->orderBy(['menu.sort' => SORT_ASC, 'menu.root' => SORT_ASC, 'menu.lft' => SORT_ASC])
    ->asArray()->all();
$menus = Menu::generateAdminlteLeftMenuTreeItems($menus);

$items = ArrayHelper::merge($items, $menus);
?>

<aside class="main-sidebar">

    <section class="sidebar">

        <!-- Sidebar user panel -->
        <div class="user-panel">
            <div class="pull-left image">
                <?= Yii::$app->user->identity->getAvatarImg(45, 45) ?>
            </div>
            <div class="pull-left info">
                <p><?= Yii::$app->user->identity->profileName ?></p>

                <a href="#"><i class="fa fa-circle text-success"></i> Online</a>
            </div>
        </div>

        <!-- search form -->
        <form action="#" method="get" class="sidebar-form">
            <div class="input-group">
                <input type="text" name="q" class="form-control" placeholder="Search..."/>
                <span class="input-group-btn">
                <button type='submit' name='search' id='search-btn' class="btn btn-flat"><i class="fa fa-search"></i>
                </button>
              </span>
            </div>
        </form>
        <!-- /.search form -->

        <?= \common\rewrite\dmstr\Menu::widget([
            'options' => ['class' => 'sidebar-menu tree', 'data-widget' => 'tree'],
            'items' => $items,
        ]) ?>

    </section>

</aside>
